/******************** (C) COPYRIGHT 2013 InMotion ******************************
*File Name   : FIR_Filter.c
*Author      : 郭盖华               
*Version     : V0.0.1
*Date        : 2013-5-1
*Description : FIR滤波文件
*******************************************************************************/
/* Includes ------------------------------------------------------------------*/
#include "fir_filter.h"
#include "stm32f4xx.h"

/* Private typedef -----------------------------------------------------------*/
/* Private define ------------------------------------------------------------*/
/* Private macro -------------------------------------------------------------*/
/* Variables -----------------------------------------------------------------*/
/* Private function prototypes -----------------------------------------------*/
/* Function ------------------------------------------------------------------*/

/*******************************************************************************
* Function Name  : FIR_Filter_int_Clear
* Description    : FIR滤波器整型数据初始化
* Input          : filter 初始化指针
* Output         : None
* Notes          : None
*******************************************************************************/
void FIR_Filter_int_Clear(FIR_FILTER_Int_DEF *filter)
{
	u32 i;
	for(i = 0 ; i < filter->tap ; i ++)
		filter->delay[i] = 0;	
}

/*******************************************************************************
* Function Name  : FIR_Filter_float_Clear
* Description    : FIR滤波器浮点型数据初始化
* Input          : filter 初始化指针
* Output         : None
* Notes          : None
*******************************************************************************/
void FIR_Filter_float_Clear(FIR_FILTER_Float_DEF *filter)
{
	u32 i;
	for(i = 0 ; i < filter->tap ; i ++)
		filter->delay[i] = 0;	
}

/*******************************************************************************
* Function Name  : FIR_Filter_int
* Description    : 整型FIR滤波器初始化
* Input          : filter 滤波器指针，invar输入值
* Output         : 滤波值
* Notes          : None
*******************************************************************************/
s32 FIR_Filter_int(FIR_FILTER_Int_DEF *filter, s32 invar) 
{
	s64 sumnum = 0;
	s32 i, *pdelay;
	const s32 *pfactor;
	pdelay = filter->delay;
	pfactor = filter->factor;
	i = filter->tap - 1;
	sumnum = 0;
	while(i--)
	{
		*pdelay = *(pdelay + 1);
		sumnum += ((s64)*pdelay) * ((s64)*pfactor);
		pdelay ++;
		pfactor ++;
	}
	*pdelay	= invar;
	sumnum += ((s64)*pdelay) * ((s64)*pfactor);
	sumnum >>= 16;
	return sumnum;
}

/*******************************************************************************
* Function Name  : FIR_Filter_float
* Description    : 浮点型FIR滤波器初始化
* Input          : filter 滤波器指针，invar输入值
* Output         : 滤波值
* Notes          : None
*******************************************************************************/
float FIR_Filter_float(FIR_FILTER_Float_DEF *filter, float invar) 
{
	float sumnum = 0;
	float *pdelay;
	u32 i;
	const float *pfactor;
	pdelay = filter->delay;
	pfactor = filter->factor;
	i = filter->tap - 1;
	sumnum = 0;
	while(i--)
	{
		*pdelay = *(pdelay + 1);
		sumnum += (*pdelay) * (*pfactor);
		pdelay ++;
		pfactor ++;
	}
	*pdelay	= invar;
	sumnum += (*pdelay) * (*pfactor);
	return sumnum;
}

/********************** (C) COPYRIGHT 2013 InMotion *****END OF FILE************/ 
